package com.pass.util;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;

public class KeyboardUtil {

    /**
     * 弹出软键盘
     * PS：若要使用此方法自动弹出软键盘，dialog样式必须配置<item name="android:windowSoftInputMode">stateVisible</item>
     */
    public static void openKeyboard(View view) {
        // 获取焦点
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        // 弹出软键盘
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }

    public static void hideKeyboard(AppCompatActivity appCompatActivity) {
        if (null == appCompatActivity) {
            return;
        }
        final View view = appCompatActivity.getCurrentFocus();
        if (null == view) {
            return;
        }
        final Object rawIME = appCompatActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null == rawIME) {
            return;
        }
        ((InputMethodManager) rawIME).hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

}
